package com.ruoyi.party.system.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.party.system.domain.Activity;
import com.ruoyi.party.system.domain.ActivitySign;
import com.ruoyi.party.system.domain.Member;
import com.ruoyi.party.system.exception.ActivitySignException;
import com.ruoyi.party.system.mapper.ActivityMapper;
import com.ruoyi.party.system.mapper.ActivitySignMapper;
import com.ruoyi.party.system.mapper.MemberMapper;
import com.ruoyi.party.system.service.IActivitySignService;

@Service
public class ActivitySignServiceImpl implements IActivitySignService {

	@Autowired
	private ActivitySignMapper activitySignMapper;
	
	@Autowired
	private MemberMapper memberMapper;
	
	@Autowired
	private ActivityMapper activityMapper;

	@Override
	public int insertActivitySign(ActivitySign sign) {
		// 验证是否能进行签到
		String actId = sign.getActId();
		Activity activity = activityMapper.selectActivityById(actId);
		String actDate = activity.getActDate();// 活动时间
		String dateRange = activity.getActDateRange();// 开会起止时间
		String date_begin = dateRange.split("-")[0];
		// 判断是当天，并且签到时间早于活动开始时间
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		String now = format.format(date);
		if(now.equals(actDate)) {
			String date_tmp = actDate +" "+ date_begin;//带日期时分秒
			SimpleDateFormat fmt2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				if(date.after(fmt2.parse(date_tmp))){
					throw new ActivitySignException("签到不能晚于活动开始时间");
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}else {
			throw new ActivitySignException("非法签到时间日");
		}
		// 验证是否为准确的党员编号
		String memberId = sign.getMemberId();
		Member member = memberMapper.selectMemberById(memberId);
		if(member == null) {
			throw  new ActivitySignException("非法党员编号");
		}
		// 先查询是否已经签到
		int num = activitySignMapper.findByActIdAndMemberId(sign);
		System.out.println("num = "+num);
		if(num > 0 ) {
			throw new ActivitySignException("你已签到");
		}
		return activitySignMapper.insertActivitySign(sign);
	}

}
